package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessRealTimePrice;
import com.digitalchina.lxnsy.api.BusinessTrainingVideo;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.VideoOptsVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.viewholder.ChannelV2VideoHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：包含两级分类
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/11/29 13:58
 * 修改备注：
 * ================================================
 */
public class ChannelV2SubclassVideoActivity extends BaseActivity {


    @Bind(R.id.recycler_right)
    EasyRecyclerView recyclerRight;
    private RecyclerArrayAdapter adapterRight;
    private List<VideoOptsVO> rightList = new ArrayList<>(); // 右侧(2级分类)

    private String pCode = "";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_channel_v2_sub);
        setTitle("农产分类");
    }

    @Override
    public void initMember() {

        // 获取视频分类
        BusinessTrainingVideo.getVideoOpts(this, mHandler);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);

        recyclerRight.setLayoutManager(gridLayoutManager);

        recyclerRight.setAdapterWithProgress(adapterRight = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ChannelV2VideoHolder(parent);
            }
        });

        gridLayoutManager.setSpanSizeLookup(adapterRight.obtainGridSpanSizeLookUp(3));

        BusinessRealTimePrice.getTopLevelType(ChannelV2SubclassVideoActivity.this, pCode, mHandler);

    }


    @Override
    public void addListener() {
        adapterRight.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent();
                intent.putExtra("selectType", (Serializable) adapterRight.getItem(position));
                setResult(998, intent);
                finish();
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_VIDEO_OPTS_SUCCESS:
                        rightList = FastJsonUtil.getListBean(msg.obj.toString(), "opts", VideoOptsVO.class);
                        adapterRight.addAll(rightList);
                        break;
                    case MSG.MSG_GET_VIDEO_OPTS_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
